import { DynamicEntity } from "../../entity/DynamicEntity";
import { UserEntity } from "../../entity/UserEntity";
import { ThinkEntity } from "../../entity/ThinkEntity";
import { SearchDao } from "../../dao/SearchDao";
import { UserDao } from "../../dao/UserDao";
import { UserServiceImpl } from "./UserServiceImpl";
import { HomeServiceImpl } from "./HomeServiceImpl";
import { DynamicServiceImpl } from "./DynamicServiceImpl";
import { TopicServiceImpl } from "./TopicServiceImpl";
import { Service, Inject } from "typedi";
import { searchValueModel} from "../../model/SearchModel";
import { SearchService } from "../SearchService";
import { TagEntity } from "../../entity/TagEntity";


@Service()
export class SearchServiceImpl implements SearchService {
    @Inject()
    private UserServiceImpl!: UserServiceImpl;
    @Inject()
    private HomeServiceImpl!: HomeServiceImpl;
    @Inject()
    private DynamicServiceImpl!: DynamicServiceImpl;
    @Inject()
    private TopicServiceImpl!: TopicServiceImpl;
    @Inject()
    private SearchDao!: SearchDao;
    @Inject()
    private UserDao!: UserDao;

    public async getSearchRecommend(
        tag_Page: number,
        tag_Count: number,
        search_Type: number
      ): Promise<any> {
        return await this.SearchDao.getSearchRecommend(
          tag_Page,
          tag_Count,
          search_Type
        );
      }
      public async getSearchThinkData(params: searchValueModel): Promise<any> {
        let think_list:ThinkEntity[] =  await this.SearchDao.getSearchThinkData(params);
        await Promise.all(
          think_list.map(async (e: ThinkEntity, thinkIndex: number)=>{
            return  await this.HomeServiceImpl.changeThink(e,params.user_Id)
          })
        ).then((res:ThinkEntity[])=>{
          think_list = res;
        })
        return think_list
      }
      public async getSearchUserData(params: searchValueModel): Promise<any> {
        let user_List:UserEntity[] = await this.SearchDao.getSearchUserData(params);
        await Promise.all(user_List.map(async (e: UserEntity, userIndex: number) => {
           return  await this.UserServiceImpl.changeUser(e,params.user_Id);
        })).then((res:UserEntity[])=>{
          user_List = res;
        })
        ;
    
        return user_List;
      }
      public async getSearchDynamicData(params: searchValueModel): Promise<any> {
        let dynamic_List:DynamicEntity[] = await this.SearchDao.getSearchDynamicData(params);
        await Promise.all(
          dynamic_List.map(async (e: DynamicEntity, dynamicIndex: number) => {
           return await this.DynamicServiceImpl.changeDynamic(e,params.user_Id);
              
          })
         ).then( (res:DynamicEntity[]) =>{
          dynamic_List = res;
         } );
        return dynamic_List;
      }

      public async getSearchTopicData(params: searchValueModel): Promise<any> {
        let Topic_List:TagEntity[] = await this.SearchDao.getSearchTopicData(params);
        let User:UserEntity | undefined = await this.UserDao.getUserInfoDao(params.user_Id);
        await Promise.all(Topic_List.map(async (e: TagEntity, TopicIndex: number) => {
           return  await this.TopicServiceImpl.changeTopic(e,User!);
        })).then((res:TagEntity[])=>{
          Topic_List = res;
        })
        ;
    
        return Topic_List;
      }
}



